<?php
namespace app\know\controller;

use app\know\model\TeachingMaterialNewWordModel;
use app\know\controller\AdminKnowBaseController;
use app\know\validate\TeachingMaterialNewWordValidate;

/**
 * Class AdminTeachingMaterialNewWordController 教材生词控制器
 * @package app\know\controller
 */
class AdminTeachingMaterialNewWordController extends AdminKnowBaseController
{

    public function delete()
    {
        $param = $this->request->param();
        $id = $this->request->param('id', '');
        
        try 
        {
            $this->validateFailError($param, 'TeachingMaterialNewWord.delete');
        } 
        catch (ValidateException $e) 
        {
            $this->error($e->getError());
        }

        $user = $this->getAdminUser();
        $param['modify_user'] = $user['user_login'];

        try 
        {
            $model = new TeachingMaterialNewWordModel();
            $model->where('id', 'in', preg_split("/[\s;,]/", $id))->delete();
            $this->opLog([
                'table_name' => $model->getTableName(),
                'sql' => $model->getLastSql(),
                'create_user' => $user['user_login'],
                'before_data' => json_encode($id),
            ]);
        } 
        catch (\Throwable $th) 
        {
            $this->error($th->getMessage());
        }
        $this->success('已删除');
    }

    public function add()
    {
        $chapter_uuid = $this->request->param('chapter_uuid', '');
        $material_uuid = $this->request->param('material_uuid', '');
        $new_word_uuids = $this->request->param('new_word_uuids', '');
    
        $arr = preg_split("/[\s;,]/", $new_word_uuids);

        try 
        {
            foreach ($arr as $v)
            {
                $item['material_uuid'] = $material_uuid;
                $item['chapter_uuid'] = $chapter_uuid;
                $item['new_word_uuid'] = $v;

                $this->validateFailError($item, 'TeachingMaterialNewWord.add');
                TeachingMaterialNewWordModel::create($item);
            }
        } 
        catch (ValidateException $e) 
        {
            $this->error($e->getError());
        }

        $this->success('添加成功');
    }


}
